<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 基础参数->数据字典
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-row>
                    <el-col :xs="24" :sm="12" :lg="6" class="pr10">
                        <el-input v-model="query.data.indexCode" placeholder="字典索引" class="handle-input mr10"></el-input>
                    </el-col>
                    <el-col :xs="24" :sm="12" :lg="6" class="pr10">
                        <el-input v-model="query.data.indexName" placeholder="索引名称" class="handle-input mr10"></el-input>
                    </el-col>
                    <el-col :xs="24" :sm="12" :lg="6" class="pr10">
                        <el-select v-model="query.data.domain" placeholder="应用域" class="handle-select">
                            <el-option v-for="item in domainList"
                                       :label="item.label"
                                       :value="item.name">
                                <span style="float: left">{{ item.label }}</span>
                                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
                            </el-option>
                        </el-select>
                    </el-col>
                    <el-col :xs="24" :sm="12" :lg="6" class="pr10">
                        <el-select v-model="query.data.status" placeholder="状态" class="handle-select">
                            <el-option v-for="item in dictStatusList"
                                       :label="item.label"
                                       :value="item.value">
                                <span style="float: left">{{ item.label }}</span>
                                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                            </el-option>
                        </el-select>
                    </el-col>
                </el-row>
                <el-row class="mt10">
                    <el-col :xs="24" :sm="12" style="text-align: left">
                        <el-button type="primary" icon="el-icon-search" @click="handleAdd">新增</el-button>
                    </el-col>
                    <el-col :xs="24" :sm="12" style="text-align: right">
                        <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
                        <el-button type="primary" icon="el-icon-refresh" @click="resetQueryData">重置</el-button>
                    </el-col>
                </el-row>
            </div>

            <el-table :data="tableData"
                      border
                      class="table"
                      ref="multipleTable"
                      header-cell-class-name="table-header"
                      @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="indexCode" label="字典索引"></el-table-column>
                <el-table-column prop="indexName" label="索引名称"></el-table-column>
                <el-table-column prop="status" label="状态"></el-table-column>
                <el-table-column prop="domain" label="应用域"></el-table-column>
                <el-table-column prop="modifier" label="操作信息"></el-table-column>
                <el-table-column label="操作" width="130" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit"
                                   @click="handleEdit(scope.$index, scope.row)">
                            编辑
                        </el-button>
                        <el-button type="text" icon="el-icon-delete" class="red"
                                   @click="handleDelete(scope.$index, scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageVo.pageNo"
                        :page-size="query.pageVo.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" width="75%" height="480px" :visible.sync="editVisible">
            <el-row>
                <el-col :sm="24" :md="12">
                    <el-card class="box-card mr10">
                        <div slot="header" class="clearfix">
                            <span>数据字典索引</span>
                        </div>
                        <el-row>
                            <el-col>
                                <el-form ref="form" :model="formData" label-width="70px">
                                    <el-form-item label="字典索引">
                                        <el-input v-model="formData.indexCode"></el-input>
                                    </el-form-item>
                                    <el-form-item label="索引名称">
                                        <el-input v-model="formData.indexName"></el-input>
                                    </el-form-item>
                                    <el-form-item label="应用域">
                                        <el-select v-model="formData.domain" placeholder="所属域" class="handle-select">
                                            <el-option v-for="item in domainList"
                                                       :label="item.label"
                                                       :value="item.name">
                                                <span style="float: left">{{ item.label }}</span>
                                                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="状态">
                                        <el-radio-group v-model="formData.status">
                                            <el-radio-button v-for="item in dictStatusList" :label="item.value">
                                                {{ item.label }}
                                            </el-radio-button>
                                        </el-radio-group>
                                    </el-form-item>
                                    <el-form-item label="备注">
                                        <el-input type="textarea" v-model="formData.remark"></el-input>
                                    </el-form-item>
                                </el-form>
                            </el-col>
                        </el-row>
                    </el-card>
                </el-col>
                <el-col :sm="24" :md="12">
                    <el-card class="box-card mr10">
                        <div slot="header" class="clearfix">
                            <span>字典明细列表</span>
                            <el-button style="float: right; padding: 3px 0" type="text" @click="handleAddItem">新增
                            </el-button>
                        </div>
                        <el-row>
                            <el-col>
                                <el-table border class="table" height="272"
                                          header-cell-class-name="table-header"
                                          :data="dictItemTableData"
                                >
                                    <el-table-column prop="orderNo" label="序号" width="50"></el-table-column>
                                    <el-table-column prop="itemCode" label="编码" width="128"></el-table-column>
                                    <el-table-column prop="itemName" label="名称" width="128"></el-table-column>
                                    <el-table-column label="操作" align="center">
                                        <template slot-scope="scope">
                                            <el-button type="text" icon="el-icon-edit"
                                                       @click="handleEditItem(scope.$index, scope.row)">
                                                编辑
                                            </el-button>
                                            <el-button type="text" icon="el-icon-delete" class="red"
                                                       @click="handleDeleteItem(scope.$index, scope.row)">
                                                删除
                                            </el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </el-col>
                        </el-row>
                    </el-card>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">保 存</el-button>
            </span>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑字典明细" :visible.sync="dictItemEditVisible" width="48%" height="360px">
            <el-form ref="form" :model="dictItemFormData" label-width="100px">
                <el-form-item label="字典编码">
                    <el-input v-model="dictItemFormData.itemCode"></el-input>
                </el-form-item>
                <el-form-item label="字典名称">
                    <el-input v-model="dictItemFormData.itemName"></el-input>
                </el-form-item>
                <el-form-item label="字典索引">
                    <el-select v-model="dictItemFormData.dictId" placeholder="归属字典索引" class="handle-select">
                        <el-option v-for="item in dictIndexList"
                                   :label="item.indexName"
                                   :value="item.id">
                            <span style="float: left">{{ item.indexName }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.id }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="序号">
                    <el-input v-model="dictItemFormData.orderNo"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeDictItem">取 消</el-button>
                <el-button type="primary" @click="handleSaveItem">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {
        fetchData,
        getData,
        saveData,
        deleteData,
        getDictItem,
        fetchItemsByPage,
        fetchDictByIndex,
        saveDictItem,
        deleteDictItem
    } from '../../api/resic/dictionary';
    import { listDomain } from '../../api/appic/appDomain';
    import { OnionUtils } from '../../utils/onion-utils';

    export default {
        name: 'basetable',
        data() {
            return {
                query: {
                    data: {
                        dictCode: '',
                        dictName: '',
                        status: '',
                        domain: ''
                    },
                    pageVo: {
                        pageNo: 1,
                        pageSize: 10
                    }
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                formData: {},
                idx: -1,
                id: -1,
                dictItemTableData: [],
                dictItemFormData: {},
                dictItemEditVisible: false,
                item_idx: -1,
                item_id: -1,
                domainList: [],
                dictStatusList: [],
                dictIndexList:[]
            };
        },
        created() {
            this.initData();
            this.getData();
        },
        //方法区
        methods: {
            //重置查询条件
            resetQueryData(){
                this.query.data = {};
            },
            //初始化页面数据
            initData() {
                //初始化下拉框
                fetchDictByIndex('onion.dict.status').then(res => {
                    this.dictStatusList = res.data['onion.dict.status'];
                });

                let domainQueryParam = {
                    param: {},
                    pageVo: { pageNo: 1, pageSize: 100 }
                };
                listDomain(domainQueryParam).then(res => {
                    this.domainList = res.data;
                });
            },
            //获取数据字典索引列表
            getData() {
                fetchData(this.query).then(res => {
                    this.tableData = res.data.rows;
                    this.pageTotal = res.data.total || 50;
                });
            },
            //触发查询按键
            handleQuery() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    deleteData(this.tableData[index].id).then(result => {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                    });
                }).catch(() => {
                    this.$alert('异常！');
                });
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 删除数据字典索引
            delAllSelection() {
                const length = this.multipleSelection.length;
                let str = '';
                this.delList = this.delList.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                }
                this.$message.error(`删除了${str}`);
                this.multipleSelection = [];
            },
            // 新增操作
            handleAdd() {
                this.formData = {};
                this.editVisible = true;
                //初始化数据字典
                this.dictItemTableData = [];
            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.formData = row;
                //初始化数据字典
                this.handleFindDictItems();
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                saveData(this.formData).then(result => {
                    this.editVisible = false;
                    this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                    this.$set(this.tableData, this.idx, result.data);
                });
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
            //获取数据字典列表
            handleFindDictItems() {
                if (!OnionUtils.isEmpty(this.formData.id)) {
                    let paramData = this.formData.id;
                    fetchItemsByPage(paramData).then(res => {
                        this.dictItemTableData = res.data.rows;
                    });
                }
            },
            //新增数据字典明细
            handleAddItem() {
                this.dictIndexList[0] = this.formData;
                this.dictItemFormData = {};
                this.dictItemFormData.dictId = this.formData.id;
                this.dictItemEditVisible = true;
            },
            //编辑数据字典明细
            handleEditItem(index, row) {
                this.dictIndexList[0] = this.formData;
                this.item_idx = index;
                this.dictItemFormData = row;
                this.dictItemEditVisible = true;
            },
            //保存数据字典明细
            handleSaveItem() {
                saveDictItem(this.dictItemFormData).then(result => {
                    this.dictItemEditVisible = false;
                    this.$message.success('保存数据字典信息成功！');
                    this.handleFindDictItems();
                });
            },
            //删除数据字典明细
            handleDeleteItem(index, row) {
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    deleteDictItem(this.dictItemTableData[index].id).then(result => {
                        this.$message.success('删除成功!');
                        this.dictItemTableData.splice(index, 1);
                    });
                }).catch(() => {
                    this.$alert('异常！');
                });
            },
            //关闭|取消数据字典明细
            closeDictItem(){
                this.handleFindDictItems();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 10px;
    }

    .handle-select {
        width: 100%;
    }

    .handle-input {
        width: 100%;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .mt10 {
        margin-top: 10px;
    }

    .pr10 {
        padding-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>